// 1、实现对圆形、三角形、长方形求面积。
// 2、利用第1题，构造3个以上图形，至少圆形、三角形、矩形各有一个，对上题的图形按照面积降序排
// 列
package main

import (
	"fmt"
	"math"
	"sort"
)

type diagram struct {
	name string
	area float64
}

type triangle struct {
	high   float64
	bottom float64
	diagram
}

type roundness struct {
	radius float64
	diagram
}

type rectangle struct {
	length float64
	width  float64
	diagram
}

type Area interface {
	getArea() float64
}

func newTriangle() *triangle {
	tr := new(triangle)
	tr.name = "Triangle"
	return tr
}

func (tr *triangle) getArea() float64 {
	if tr.high <= 0 || tr.bottom <= 0 {
		return 0
	}
	return tr.high * tr.bottom / 2
}

func newRoundness() *roundness {
	ro := new(roundness)
	ro.name = "Roundness"
	return ro
}

func (ro *roundness) getArea() float64 {
	if ro.radius <= 0 {
		return 0
	}
	return math.Pi * math.Pow(ro.radius, 2)
}

func newRectangle() *rectangle {
	re := new(rectangle)
	re.name = "Rectangle"
	return re
}

func (re *rectangle) getArea() float64 {
	if re.length <= 0 || re.width <= 0 {
		return 0
	}
	return re.length * re.width
}

func main() {
	tr := newTriangle()
	tr.bottom = 4.9
	tr.high = 3.6
	tr.area = tr.getArea()
	fmt.Printf("三角形面积为：%f\n", tr.area)
	ro := newRoundness()
	ro.radius = 6
	ro.area = ro.getArea()
	fmt.Printf("圆形面积为：%f\n", ro.area)
	re := newRectangle()
	re.length = 6.3
	re.width = 7.9
	re.area = re.getArea()
	fmt.Printf("长方形面积为: %f\n", re.area)

	di := make(map[string]float64)
	di[tr.name] = tr.area
	di[ro.name] = ro.area
	di[re.name] = re.area
	diagrams := make([]diagram, len(di))
	i := 0
	for k, v := range di {
		diagrams[i] = diagram{k, v}
		i++
	}
	sort.Slice(diagrams, func(i, j int) bool {
		return diagrams[i].area > diagrams[j].area
	})
	fmt.Println("三个图形面积降序排序为：", diagrams)
}
